import Nunjucks from 'nunjucks'
import { fileURLToPath } from 'url'

//暴露 注册模板引擎
export const registerTemplate = () => {
    return async (ctx, next) => {
        ctx.render = (html, obj) => {
            let viewUrl = new URL('../view', import.meta.url)
            let viewPath = fileURLToPath(viewUrl)
            let env = Nunjucks.configure(viewPath)
            ctx.response.body = env.render(html, obj)
        }
        await next();
    }
}